/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.network.request.builder.RegRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.RegResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.RegResponseHandler;

/**
 * @author wdy
 *
 */
public class RegActivity extends BaseActivity implements OnResponseListener,
		OnClickListener {
	private Context mContext;
	private static final String TAG = RegActivity.class.getSimpleName();
	private EditText mId;
	private EditText mPwd;
	private EditText mPwd2;

	private RegRequestBuilder mReq = new RegRequestBuilder();
	private RegResponseHandler mHandler = new RegResponseHandler();
	
	public String username;
	public String pwd;
	public EditText cartype;
	public EditText carnumber;
	public EditText enginenumber;
	public EditText carbonenumber;
	public TextView baoyang;
	public TextView nianjian;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reg);
		
		mContext = this;
		initViews();
		reg();
	}

	@Override
	public void initViews() {
		super.initViews();
		mId = (EditText) findViewById(R.id.reg_id);
		mPwd = (EditText) findViewById(R.id.reg_pwd);
		mPwd2 = (EditText) findViewById(R.id.reg_pwd2);
	}

	@Override
	public void reg() {
		super.reg();
		findViewById(R.id.reg_ok).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mHandler.setOnResponseListener(this);
	}

	@Override
	public void initData() {
		super.initData();
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		if(((RegResponse)classz.getResponse()).user!=null){
			new UserDao().saveUser(((RegResponse)classz.getResponse()).user);
			Toast.makeText(mContext, "注册成功！", Toast.LENGTH_LONG).show();
			setResult(Activity.RESULT_OK);
			finish();
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "注册失败！", Toast.LENGTH_LONG).show();
	}

	private boolean validInput() {
		if (mId.getText().toString().length() == 0) {
			Toast.makeText(mContext, "请输入账号", Toast.LENGTH_LONG).show();
			return false;
		}
		if (mPwd.getText().toString().length() == 0) {
			Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_LONG).show();
			return false;
		}
		if (!mPwd.getText().toString().equals(mPwd2.getText().toString())) {
			Toast.makeText(mContext, "两次密码不一致", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.reg_ok:
			if (validInput()) {
				mReq.setUsername(mId.getText().toString())
					.setPwd(mPwd.getText().toString())
					.postJsonRequest(mContext, mHandler);
			}
			break;
		case R.id.back:
			finish();
			break;
		}
	}

}
